package com.enqualcomm.kids.mvp.fencing;

import android.content.Context;

import com.android.volley.NetworkResponse;
import com.android.volley.socket.SocketClient;
import com.android.volley.socket.SocketNetwork;
import com.enqualcomm.kids.network.Parser;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.FencingListParams;
import com.enqualcomm.kids.network.socket.response.FencingListResult;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yinzhiqun on 2016/3/2.
 */
public class FencingModel {

    public Observable<FencingListResult> getFencings(final String userkey,final String terminalid,Context context){
        final SocketNetwork socketNetwork = SocketClient.initSocketNetwork(context);
        return Observable.create(new Observable.OnSubscribe<FencingListResult>() {
            @Override
            public void call(Subscriber<? super FencingListResult> subscriber) {
                FencingListResult fencingListResult = null;
                try {
                    NetworkResponse networkResponse = socketNetwork.performRequest(new SocketRequest<FencingListResult>(new FencingListParams(userkey, terminalid), null));
                    fencingListResult = Parser.parse(networkResponse, FencingListResult.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if (fencingListResult!=null){
                    subscriber.onNext(fencingListResult);
                    subscriber.onCompleted();
                }else{
                    subscriber.onError(null);
                }
            }
        }).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
    }

}
